/**
 * Created with IntelliJ IDEA.
 * User: duoba
 * Date: 16/2/23
 * Time: 下午2:31
 * To change this template use File | Settings | File Templates.
 */

package com.cake21.uitest.System.Core;

import java.util.Set;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;

public class Utils {
    /*切换浏览器窗口*/
    public static boolean switchToWindow(WebDriver driver, String windowTitle) {
        boolean flag = false;
        try {
            String currentHandle = driver.getWindowHandle();
            Set<String> handles = driver.getWindowHandles();
            for (String s : handles) {
                if (s.equals(currentHandle))
                    continue;
                else {
                    driver.switchTo().window(s);
                    if (driver.getTitle().contains(windowTitle)) {
                        flag = true;
                        System.out.println("切换到浏览器窗口: " + windowTitle + " 成功!");
                        break;
                    } else
                        continue;
                }
            }
        } catch (NoSuchWindowException e) {
            System.out.println("浏览器窗口: " + windowTitle + " 没找到!");
            flag = false;
        }
        return flag;
    }

    /*为截全屏做准备,scroll到页面最下面再回到顶端*/
    public static void GetReadyForFullPageScreenShot(WebDriver driver) {
        try {
            JavascriptExecutor jse = (JavascriptExecutor) driver;
            jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
            Thread.sleep(500);
            jse.executeScript("window.scrollTo(document.body.scrollHeight, 0)");
            Thread.sleep(500);
            jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");
            Thread.sleep(500);
            jse.executeScript("window.scrollTo(Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight),0);");
            Thread.sleep(500);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
